﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.Samples.ServiceHosting.StorageClient;

namespace Ciripescu_WebRole.BL
{
    public enum MailType
    {
        Welcome,
        Invite,
        CiripUpdate,
        UrmaritorNou,
        ReportToDevTeam,
        ResetPass
    }

    public class MailQueue
    {
        StorageAccountInfo account;
        QueueStorage service;
        MessageQueue scheduledQueue;
        MessageQueue urgentQueue;
        /// <summary>
        /// Acest factor zice pe ce perioada de timp se va imprastia trimierea mailurilor prezente acuma in coada
        /// Atentie:
        /// pt DeliveryTimeFactor = 12:
        /// 1 mail = 12 ore;
        /// 2 mailuri = 18 ore;
        /// 3 = 22  ore;
        /// 10 = 35 ore;
        /// 100 = 62 ore;
        /// 1000 = 89 ore;
        /// 10 mii = 117 ore;
        /// 100 mii = 145 ore;
        /// 1 mil = 172 ore;
        /// 10 mil = 227 ore;
        /// 100 mil = 255 ore;
        /// Timpul necesar este liniar cu valoarea DeliveryTimeFactor (adica daca se ia 6 in loc de 12 timpul va scadea la jumate)
        /// </summary>
        public static int DeliveryTimeFactor = 12; //ore
        public static string ScheduledQueueName = "emailstosend";
        public static string UrgentQueueName = "urgentemails";
        public MailQueue()
        {
            account = StorageAccountInfo.GetDefaultQueueStorageAccountFromConfiguration();
            service = QueueStorage.Create(account);
            scheduledQueue = service.GetQueue(ScheduledQueueName);
            urgentQueue = service.GetQueue(UrgentQueueName);
        }

        public static double HoursNeededToSendEmails(int nr)
        {
            double timp = 0;
            int nbEmails = nr;
            while (nbEmails > 0)
            {
                double interval = (double)DeliveryTimeFactor / (double)nbEmails;
                timp += interval;
                nbEmails--;
            }
            return timp;
        }

        public void SendInvitation(string rcptName, string rcptEmail,string senderName,string senderEmail)
        {
            //message:type, pe cine invit-nume/adresa, cine invita-nume/adresa
            string msg = string.Format("{0}/{1}/{2}/{3}/{4}",
                                             MailType.Invite,
                                             rcptName,
                                             rcptEmail,
                                             senderName,
                                             senderEmail);
            Message newMail = new Message(msg);
            scheduledQueue.PutMessage(newMail);

        }

        public void SendNewUrmaritorNotification(string rcptName, string rcptEmail, string senderName, string senderEmail,string rcptUser, string senderUser)
        {
            //message:type, pe cine invit-nume/adresa, cine invita-nume/adresa
            string msg = string.Format("{0}/{1}/{2}/{3}/{4}/{5}/{6}",
                                             MailType.UrmaritorNou,//{0}
                                             rcptName,//{1}
                                             rcptEmail,//{2}
                                             senderName,//{3}
                                             senderEmail,//{4}
                                             rcptUser,//{5}
                                             senderUser//{6}
            );
            Message newMail = new Message(msg);
            scheduledQueue.PutMessage(newMail);

        }

        public void SendWelcomeMessage(string rcptEmail, string rcptName)
        {
            string msg = string.Format("{0}/{1}/{2}", MailType.Welcome, rcptName,rcptEmail);
            Message newMail = new Message(msg);
            urgentQueue.PutMessage(newMail);
        }

        public void SendResetPassMail(string rcptEmail, string rcptName, string confirmationKey)
        {
            string msg = string.Format("{0}/{1}/{2}/{3}", MailType.ResetPass, rcptName, rcptEmail,confirmationKey);
            Message newMail = new Message(msg);
            urgentQueue.PutMessage(newMail);
        }

        public void ReportCriticalError(string message)
        {
            string m = message;
            // ce sec ar fi sa se intrerupa un mesaj important din cauza ca contine slash
            // inlocuitor in stil chinezesc/romanesc pt base64 encode (se vezi acuma buguri):
            while (m.Contains("/"))
                m = m.Replace("/", "[slash]");

            // TODO: cand trece lenea muta adresa de email in web.config
            string msg = string.Format("{0}/{1}/{2}/{3}", MailType.ReportToDevTeam, "Dev Team", "tudor.carean@bitstarsolutions.com",m);
            Message newMail = new Message(msg);
            urgentQueue.PutMessage(newMail);
        }
    }

    

}
